﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using ExtAspNet;

namespace HY.WEB.SYS
{
    public partial class _xmenu : PageBase
    {
        public string whe1 = "", whe2 = "", whe3 = "", stype = "";
        public string us ;
        public string tab = "sys_menu";
        protected void Page_Load(object sender, EventArgs e)
        {  
            us=Session["ID"].ToString();
            if (!IsPostBack)
            {              
              
                BindGrid();  
                fill();
                //txtbh.Text = getmaxbh("");
            }
        }

        private void fill()
        {
         


            ddlmenu2.Items.Clear();
            DataTable du = BLL.Bllbase.get_table(tab,whe1);
            for (int i = 0; i < du.Rows.Count; i++)
            {
                ddlmenu2.Items.Add(new ExtAspNet.ListItem(du.Rows[i]["m_name"].ToString(), du.Rows[i]["sid"].ToString()));
            }
            ddlmenu2.DataBind();
          //  ddlmenu2.Items.Add(new ExtAspNet.ListItem("", ""));


            ddlmenu3.Items.Clear();
            du = BLL.Bllbase.get_table(tab, " owenid>0 and lev=0 ");
            for (int i = 0; i < du.Rows.Count; i++)
            {
                ddlmenu3.Items.Add(new ExtAspNet.ListItem(du.Rows[i]["m_name"].ToString(), du.Rows[i]["sid"].ToString()));
            }
            ddlmenu3.DataBind();

           
        }

        public  string getnm(string nmid)
        {
            BLL.sys.sys_user u = new BLL.sys.sys_user();
            return u.getname(nmid);
         }

        private void BindGrid()
        {
            DataTable table1, table2, table3;
            whe1=" owenid=0";
            table1 = BLL.Bllbase.get_table(tab,whe1);
            Grid1.DataSource = table1;
            Grid1.DataBind();


            whe2 = " owenid >0 and owenid  not in(select sid from sys_menu where owenid>0 ) ";
            table2 = BLL.Bllbase.get_table(tab,whe2 );
            Grid2.DataSource = table2;
            Grid2.DataBind();


            whe3= " owenid  in(select sid from sys_menu where owenid >0 ) ";
            table3 = BLL.Bllbase.get_table(tab,whe3);
            Grid3.DataSource = table3;
            Grid3.DataBind();
         
        }


        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
        }

        protected void Grid2_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid2.PageIndex = e.NewPageIndex;
        }

        protected void Grid3_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid3.PageIndex = e.NewPageIndex;
        }


        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            whe1 = " owenid=0";
            DataTable table = BLL.Bllbase.get_table(tab,whe1);
            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);
            Grid1.DataSource = view1;
            Grid1.DataBind();
        }

        protected void Grid2_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            whe2 = " owenid >0 and owenid  not in(select sid from sys_menu where owenid>0 ) ";
            DataTable table = BLL.Bllbase.get_table(tab,whe2);
            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);
            Grid2.DataSource = view1;
            Grid2.DataBind();
        }

        protected void Grid3_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            whe3 = " owenid  in(select sid from sys_menu where owenid >0 ) ";
            DataTable table = BLL.Bllbase.get_table(tab,whe3);
            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);
            Grid3.DataSource = view1;
            Grid3.DataBind();
        }

        //删除一级表
        protected void btnDelete1_Click(object sender, EventArgs e)
        {
            int val = Grid1.PageIndex * Grid1.PageSize;
            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid1.SelectedRowIndexArray)
            {
                string DD = Grid1.DataKeys[row + val][0].ToString();
                sb.Append(Grid1.DataKeys[row][0].ToString());
                sb.Append(",");
            }


            Boolean bval = HY.BLL.Bllbase.CheckList(tab, sb.ToString().TrimEnd(','));
            if (!bval)
            {
                Alert.ShowInTop("你选择了删除项：" + sb.ToString().TrimEnd(','));
                HY.BLL.Bllbase.DeleteList(tab, sb.ToString().TrimEnd(','));
            }
            else
                Alert.ShowInTop("含有子数据,不能删除");

            BindGrid();
        }
    
        //删除二级表
        protected void btnDelete2_Click(object sender, EventArgs e)
        {
            int val = Grid2.PageIndex * Grid2.PageSize;
            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid2.SelectedRowIndexArray)
            {
               string DD = Grid2.DataKeys[row+val][0].ToString();
                sb.Append(Grid2.DataKeys[row][0].ToString());
                sb.Append(",");
            }


            Boolean bval = HY.BLL.Bllbase.CheckList(tab, sb.ToString().TrimEnd(','));
            if (!bval)
            {
                Alert.ShowInTop("你选择了删除项：" + sb.ToString().TrimEnd(','));
                HY.BLL.Bllbase.DeleteList(tab, sb.ToString().TrimEnd(','));
            }
            else
                Alert.ShowInTop("含有子数据,不能删除");

            BindGrid();
        }

        //删除三级表
        protected void btnDelete3_Click(object sender, EventArgs e)
        {
            int val = Grid3.PageIndex * Grid3.PageSize;

            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid3.SelectedRowIndexArray)
            {
                sb.Append(Grid3.DataKeys[row+val][1].ToString());
                sb.Append(",");
            }


                Alert.ShowInTop("你选择了删除项：" + sb.ToString().TrimEnd(','));
                HY.BLL.Bllbase.DeleteList(tab, sb.ToString().TrimEnd(','));
         

            BindGrid();

        }

        protected void Grid1_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            string DD = Grid1.DataKeys[e.RowIndex][0].ToString();
       

            if (e.CommandName == "sub") //查询 
            {
                DataTable dt1 = BLL.Bllbase.get_table(tab, " owenid='" + DD + "'");
                Grid2.PageIndex = 0;
                Grid2.DataSource = dt1;
                Grid2.DataBind();
                
            }

            if (e.CommandName == "edit") //修改 
            {                
                DataTable dt1 = BLL.Bllbase.get_table(tab, "sid=" + DD + "");
                hidsid.Text = dt1.Rows[0]["sid"].ToString().Trim();
             
               
                txtname.Text = dt1.Rows[0]["m_name"].ToString().Trim();
                txtord.Text = dt1.Rows[0]["isorder"].ToString().Trim();
                txtlink.Text = dt1.Rows[0]["m_link"].ToString().Trim();
               
                if (dt1.Rows[0]["showstatus"].ToString().Trim() == "1") 
                    chbsh.Checked = true;
                else
                    chbsh.Checked = false;

                if (dt1.Rows[0]["lev"].ToString().Trim() == "1") 
                    chblev.Checked = true;
                else
                    chblev.Checked = false;

            }
        }

        protected void Grid2_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {

            string DD = Grid2.DataKeys[e.RowIndex][0].ToString();
        
            
            if (e.CommandName == "sub") //查询 
            {

                DataTable dt1 = BLL.Bllbase.get_table(tab, " owenid='" + DD + "'");
                Grid3.PageIndex = 0;                
                Grid3.DataSource = dt1;
                Grid3.DataBind();
               
            }

            if (e.CommandName == "edit") //修改 
            {               
                DataTable dt1 = BLL.Bllbase.get_table(tab,"sid=" + DD + "");
                hidsid2.Text = dt1.Rows[0]["sid"].ToString().Trim();
            
                string t = dt1.Rows[0]["owenid"].ToString().Trim();

                ddlmenu2.SelectedIndex = ddlmenu2.Items.IndexOf(ddlmenu2.Items.FindByValue(t));

                hidsid2.Text = dt1.Rows[0]["sid"].ToString().Trim();

                txtname2.Text = dt1.Rows[0]["m_name"].ToString().Trim();
                txtord2.Text = dt1.Rows[0]["isorder"].ToString().Trim();
                txtlink2.Text = dt1.Rows[0]["m_link"].ToString().Trim();

                if (dt1.Rows[0]["showstatus"].ToString().Trim() == "1")
                    chbsh2.Checked = true;
                else
                    chbsh2.Checked = false;

                if (dt1.Rows[0]["lev"].ToString().Trim() == "1")
                    chblev2.Checked = true;
                else
                    chblev2.Checked = false;
                                

           
            }
            

        }

        protected void Grid3_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            string DD = Grid3.DataKeys[e.RowIndex][0].ToString();
        
            if (e.CommandName == "edit") //修改 
            {

                DataTable dt1 = BLL.Bllbase.get_table(tab, "sid=" + DD + "");
                string t = dt1.Rows[0]["owenid"].ToString().Trim();

                ddlmenu3.SelectedIndex = ddlmenu2.Items.IndexOf(ddlmenu2.Items.FindByValue(t));

                hidsid3.Text = dt1.Rows[0]["sid"].ToString().Trim();

                txtname3.Text = dt1.Rows[0]["m_name"].ToString().Trim();
                txtord3.Text = dt1.Rows[0]["isorder"].ToString().Trim();
                txtlink3.Text = dt1.Rows[0]["m_link"].ToString().Trim();

                if (dt1.Rows[0]["showstatus"].ToString().Trim() == "1")
                    chbsh3.Checked = true;
                else
                    chbsh3.Checked = false;

                if (dt1.Rows[0]["lev"].ToString().Trim() == "1")
                    chblev3.Checked = true;
                else
                    chblev3.Checked = false;
                                

            }
        }

    

        protected void btsave1_Click(object sender, EventArgs e)
        {
            string nm=txtname.Text;
            string ord = txtord.Text;
            string tlink = txtlink.Text;
            string cbhss = "0",cbhll = "0";
            string sqltxt = "";
            string sid = hidsid.Text;
          

            if (chbsh.Checked) cbhss = "1";
            if (chblev.Checked) cbhll = "1";

            if (hidsid.Text== "")
            {
                sqltxt = "insert into " + tab + "(owenid,lev,m_name,m_link,showstatus,isorder,TreeLevel)  values(0," +
                                                cbhll + ",'" +
                                                nm + "','" +
                                                tlink + "'," +
                                                cbhss + "," +
                                                ord + ",0)";
            }
            else
            {
                sqltxt = "update  " + tab + "  set TreeLevel=0,lev=" + cbhll + 
                                                ",m_name='" + nm +
                                               "',m_link='" + tlink +
                                               "',showstatus=" + cbhss +
                                               ",isorder=" + ord +
                                               "  where sid="+sid+"";

            }

            DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);
            sqltxt = "update  " + tab + " set remark=sid where m_name='" + nm + "'";
            DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);

            hidsid.Text = "";

           // ddlarea.SelectedIndex = ddlarea.Items.IndexOf(ddlarea.Items.FindByValue(""));
          
            txtname.Text = "";
            txtord.Text = "1";
            txtlink.Text = "";

            BindGrid();
        }

        protected void btsave2_Click(object sender, EventArgs e)
        {
      
            string menu2 = ddlmenu2.SelectedValue;
      
            string nm = txtname2.Text;
            string ord = txtord2.Text;
            string tlink = txtlink2.Text;
            string cbhss = "0", cbhll = "1";
            string sqltxt = "";
            string sid = hidsid2.Text;


            if (chbsh2.Checked) cbhss = "1";
            if (chblev2.Checked) cbhll = "1";

            if (hidsid2.Text == "")
            {
                sqltxt = "insert into " + tab + "(owenid,lev,m_name,m_link,showstatus,isorder,TreeLevel)  values(" +
                                                   menu2 + "," +
                                                    cbhll + ",'" +
                                                   nm + "','" +
                                                   tlink + "'," +
                                                   cbhss + "," +
                                                   ord + ",1)";
            }
            else
            {
                sqltxt = "update  " + tab + "  set TreeLevel=1,owenid=" + menu2 +
                                                ",lev=" + cbhll +
                                                ",m_name='" + nm +
                                               "',m_link='" + tlink +
                                               "',showstatus=" + cbhss +
                                               ",isorder=" + ord +
                                               "  where sid=" + sid+ "";

            }

            DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);

            sqltxt = "update  " + tab + " set remark=sid where m_name='" + nm + "'";
            DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);

            hidsid2.Text = "";

            // ddlarea.SelectedIndex = ddlarea.Items.IndexOf(ddlarea.Items.FindByValue(""));

            txtname2.Text = "";
            txtord2.Text = "1";
            txtlink2.Text = "";

            BindGrid();
        }

        protected void btsave3_Click(object sender, EventArgs e)
        {
            string menu3 = ddlmenu3.SelectedValue;

            string nm = txtname3.Text;
            string ord = txtord3.Text;
            string tlink = txtlink3.Text;
            string cbhss = "0", cbhll = "1";
            string sqltxt = "";
            string sid = hidsid3.Text;


            if (chbsh3.Checked) cbhss = "1";
            if (chblev3.Checked) cbhll = "1";

            if (hidsid3.Text == "")
            {
                sqltxt = "insert into " + tab + "(owenid,lev,m_name,m_link,showstatus,isorder,TreeLevel)  values(" +
                                                   menu3 + "," +
                                                    cbhll + ",'" +
                                                   nm + "','" +
                                                   tlink + "'," +
                                                   cbhss + "," +
                                                   ord + ",2)";
            }
            else
            {
                sqltxt = "update  " + tab + "  set TreeLevel=2,owenid=" + menu3 +
                                                ",lev=" + cbhll +
                                                ",m_name='" + nm +
                                               "',m_link='" + tlink +
                                               "',showstatus=" + cbhss +
                                               ",isorder=" + ord +
                                               "  where sid=" +sid + "";

            }

            DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);

            sqltxt = "update  " + tab + " set remark=sid where m_name='" + nm + "'";
            DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);

            hidsid3.Text = "";

            // ddlarea.SelectedIndex = ddlarea.Items.IndexOf(ddlarea.Items.FindByValue(""));

            txtname3.Text = "";
            txtord3.Text = "1";
            txtlink3.Text = "";
            BindGrid();
        }


    }
}
